<template>
  <div class="photoArea">
    <canvas v-if="configHead.type=='move'" id="bargraphCanvas" :width="configHead.width" :height="configHead.height"
      style="border:1px solid #ccc;"></canvas>
    <div class="show" v-if="configHead.type=='show'" ref="photoArea"
      :style="{width:Number(configHead.width)/Number(configHead.height)*boxHeight+'px'}">
      <div class="photo" :style="{backgroundImage:'url('+configHead.imgSrc+')'}"></div>
      <div class="point_list">
        <div v-for="(item, key) in configData" :key="key" class="point" @click="forwards(configHead.clickFun,item)"
          :style="{backgroundImage:'url('+item[configHead.conList.icon]+')',left:(Number(item[configHead.conList.left])+Number(iconWidth)/2)/ Number(configHead.width) * Number(boxWidth)-Number(iconWidth)/2+'px',top:(Number(item[configHead.conList.top])+Number(iconHeight)/2)/ Number(configHead.height) * Number(boxHeight)-Number(iconHeight)/2+'px'}">
          <div class="con">

            <div class="box_bg">
              <div class="title">{{item[configHead.conList.title]}}</div>
              <div class="li" :title="item[configHead.conList.deviceSn]"><span class="label">设备编号 :
                </span>{{item[configHead.conList.deviceSn]}}</div>
              <div class="li"><span class="label">设备状态
                  : </span>{{configHead.conList.filter[item[configHead.conList.status]]}}</div>
            </div>
          </div>
        </div>
      </div>
      <!--  -->
    </div>
  </div>
</template>
<style scoped>
  .photoArea {
    width: 100%;
    height: 100%;
    overflow: initial;
  }


  .show {
    position: relative;
    height: 100%;
    margin: 0 auto;
  }

  .show .photo {
    width: 100%;
    height: 100%;
    background: center no-repeat;
    background-size: 100% 100%;
  }

  .show .point_list {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    z-index: 5;
  }

  .show .point_list .point {
    position: absolute;
    background: center no-repeat;
    width: 30px;
    height: 30px;
    cursor: pointer;
    background-size: 100% 100%;
  }

  .show .point_list .point .con {
    display: none;
    width: 150px;
    height: 88px;
    background: rgba(11, 91, 147, .5);
    border-radius: 5px;
    overflow: hidden;
    padding: 0 10px 11px 10px;
    position: absolute;
    z-index: 10;
    left: 30px;
    top: 0px;
  }

  .show .point_list .point .con .title {
    font-size: 14px;
    color: #fff;
    height: 34px;
    line-height: 34px;
    border-bottom: 1px solid #fff;
    box-sizing: border-box;
  }

  .show .point_list .point .con .li {
    font-size: 12px;
    color: #fff;
    margin-top: 10px;
    overflow: hidden;
    text-overflow: ellipsis;
    width: 100%;
    white-space: nowrap;
  }

  .show .point_list .point:hover .con {
    display: block;
  }

</style>
<style>

</style>

<script>
  import axios from 'axios'

  export default {
    name: 'leftNav',
    data() {
      return {
        boxHeight: 0,
        boxWidth: 0,
        iconWidth: 40,
        iconHeight: 40
      }
    },
    created() {},
    watch: {
      configHead: {
        handler(newName, oldName) {
          if (this.$refs.photoArea) {
            this.boxHeight = this.$refs.photoArea.offsetHeight;
            this.boxWidth = Number(this.configHead.width) / Number(this.configHead.height) * this.boxHeight;
          }
          this.$nextTick(function () {
            this.changePosition();
          })

        },
        immediate: true,
        deep: true
      }

    },
    mounted() {


      window.onresize = () => {
        if (this.$refs.photoArea) {
          this.boxHeight = this.$refs.photoArea.offsetHeight;
        }
        this.boxWidth = Number(this.configHead.width) / Number(this.configHead.height) * this.boxHeight;

      }

      if (this.$refs.photoArea) {
        this.boxHeight = this.$refs.photoArea.offsetHeight;
        // this.boxWidth = this.$refs.photoArea.offsetWidth;
        this.boxWidth = Number(this.configHead.width) / Number(this.configHead.height) * this.boxHeight;
      }
      var canvas, context;

      var img, //图片对象
        imgIsLoaded, //图片是否加载完成;
        imgX = this.configHead.left,
        imgY = this.configHead.top,
        imgScale = 1;
      var imgBg;
      var self = this;

      canvas = document.getElementById('bargraphCanvas'); //画布对象
      if (!canvas) {
        return false;
      }
      context = canvas.getContext('2d'); //画布显示二维图片pos

      function drawImage() {
        context.clearRect(0, 0, canvas.width, canvas.height);
        context.globalCompositeOperation = "source-over";
        context.drawImage(imgBg, 0, 0, canvas.width, canvas.height);


        context.drawImage(
          img, //规定要使用的图像、画布或视频。
          0, 0, //开始剪切的 x 坐标位置。
          50, 50, //被剪切图像的高度。
          imgX, imgY, //在画布上放置图像的 x 、y坐标位置。
          img.width * imgScale, img.height * imgScale //要使用的图像的宽度、高度
        );
      }
      /*坐标转换*/
      function windowToCanvas(x, y) {
        var box = canvas.getBoundingClientRect(); //这个方法返回一个矩形对象，包含四个属性：left、top、right和bottom。分别表示元素各边与页面上边和左边的距离
        return {
          x: x - box.left - (box.width - canvas.width) / 2,
          y: y - box.top - (box.height - canvas.height) / 2
        };
      }
      /*事件注册*/
      function canvasEventsInit() {
        canvas.onmousedown = function (event) {
          //						var pos = windowToCanvas(event.clientX, event.clientY); //坐标转换，将窗口坐标转换成canvas的坐标

          canvas.onmousemove = function (evt) { //移动
            canvas.style.cursor = 'move';
            var posl = windowToCanvas(evt.clientX, evt.clientY);
            //							var x = posl.x - pos.x;
            //							var y = posl.y - pos.y;
            //							pos = posl;
            // imgX = posl.x;
            // imgY = posl.y;

            imgX = posl.x - img.width * imgScale / 2;
            imgY = posl.y - img.height * imgScale / 2;
            //							$(".fixed").css({"left":imgX,"top":imgY});
            self.$emit('moveFun', [imgX, imgY]);
            drawImage(); //重新绘制图片
          };
          canvas.onmouseup = function () {
            canvas.onmousemove = null;
            canvas.onmouseup = null;
            canvas.style.cursor = 'default';
          };
        };

        // canvas.onmousewheel = canvas.onwheel = function (event) { //滚轮放大缩小

        //   var pos = windowToCanvas(event.clientX, event.clientY);
        //   event.wheelDelta = event.wheelDelta ? event.wheelDelta : (event.deltalY * (-40)); //获取当前鼠标的滚动情况
        //   if (event.wheelDelta > 0) {
        //     imgScale *= 2;
        //     imgX = imgX * 2 - pos.x;
        //     imgY = imgY * 2 - pos.y;
        //   } else {
        //     imgScale /= 2;
        //     imgX = imgX * 0.5 + pos.x * 0.5;
        //     imgY = imgY * 0.5 + pos.y * 0.5;
        //   }
        //   drawImage(); //重新绘制图片
        // };
      }

      canvasEventsInit();

      function loadImg() {
        img = new Image();

        img.src = self.configHead.icon;
        img.width = 40;
        img.height = 40;
        if (self.configHead.left > self.configHead.width - img.width / 2 || self.configHead.left == self.configHead
          .width - img.width /
          2) {
          imgX = self.configHead.width - img.width
        }
        if (self.configHead.top > self.configHead.height - img.height / 2 || self.configHead.top == self.configHead
          .height - img
          .height / 2) {
          imgY = self.configHead.height - img.height
        }
        img.onload = function () {
          imgIsLoaded = true;
          drawImage();
        }
      }

      function loadBg() {
        var into = document.createElement("canvas");
        into.width = canvas.width;
        into.height = canvas.height;
        var ctx2 = into.getContext('2d');
        imgBg = new Image();
        imgBg.src = self.configHead.imgSrc;
        imgBg.onload = function () {
          ctx2.drawImage(imgBg, 0, 0, canvas.width, canvas.height);
          context.globalCompositeOperation = "destination-over";
          context.drawImage(imgBg, 0, 0, canvas.width, canvas.height);
          var pattern = context.createPattern(into, 'no-repeat');
          context.fillStyle = pattern;
          context.fillRect(0, 0, canvas.width, canvas.height);
        };
      }
      if (canvas) {
        loadImg();
        loadBg();
      }
    },
    methods: {
      forwards(fun, scope) {
        fun(scope)
      },
      changePosition() {
        let pointList = document.getElementsByClassName("point");
        for (let i = 0; i < pointList.length; i++) {

          let con = pointList[i].querySelector(".con");
          if (pointList[i].offsetTop < 0) {
            con.style.top = "25px";
            con.style.bottom = "unset";
          }
          if (pointList[i].offsetTop > this.boxHeight - this.iconHeight) {
            con.style.top = "unset";
            con.style.bottom = "25px";
          }
          if (pointList[i].offsetLeft > this.boxWidth - this.iconWidth - 180) {
            con.style.left = "unset";
            con.style.right = this.iconWidth + "px";
          }
        }
      }
    },
    props: ['configHead', 'configData']
  }

</script>
